/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.data;

import com.tlf.dyjc5.service.YsjService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dusin.commonutil.DateUtil;
import org.dusin.web.util.CommonWebUtil;
import org.dusin.web.util.JsonUtil;
import org.dusin.web.util.TheUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author tengyu
 */
public class YsjLoad extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String otherProperties= "";
        
        try {
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            YsjService ysjService = (YsjService) context.getBean("ysjService");

            String command = request.getParameter("cmd");
            List<Map<String, Object>> list =null;
            String currentOrgCode = (String) request.getSession().getAttribute("currentOrgCode");
            String lock = request.getParameter("lock");
            int start = CommonWebUtil.getIntParameter(request.getParameter("start"), 0);
            int limit = CommonWebUtil.getIntParameter(request.getParameter("limit"), 0);
            if ("general".equals(command)) {
            
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                String condition = TheUtil.getPointSelectCondition(request);
                boolean isLock = ysjService.isLock(currentOrgCode, Integer.parseInt(dateTo.substring(0,4)), Integer.parseInt(dateTo.substring(4)),lock);
                otherProperties = "isLock:"+isLock;
                list = ysjService.findAll(currentOrgCode, dateFrom, dateTo, condition, start, limit,lock);
            }if ("generalLock".equals(command)) {                //t_ysjLock加载
            
                String beginTimeStr = request.getParameter("beginDate");
                String endTimeStr = request.getParameter("endDate");
                String condition = TheUtil.getPointSelectCondition(request);
                //sb.append(" and startStatus=1");
                list = ysjService.findAllLock2(currentOrgCode, beginTimeStr, endTimeStr, condition, start, limit);
            }else if ("recall".equals(command)) {                
            
                String date = request.getParameter("endDate");               
                
                String condition = TheUtil.getPointSelectCondition(request);
                
                list = ysjService.findAllDataRecall(currentOrgCode,  date, condition, start, limit);
            }else if ("recallLock".equals(command)) {                        //锁定月数据维护            
            
                String date = request.getParameter("endDate");                 
                String condition = TheUtil.getPointSelectCondition(request);                     
                list = ysjService.findAllLock2(currentOrgCode,  date,date, condition, start, limit);              
                
            }else if ("lock".equals(command)) {    //锁定月数据时显示的数据    ---监测点            
            
                String date = request.getParameter("endDate");               
                
                String condition = TheUtil.getPointSelectCondition(request);
                
                list = ysjService.findAllLock(currentOrgCode,  date, condition, start, limit);
            }else if("rangeData".equals(command)){                
            
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                long pointId = Long.parseLong(request.getParameter("pointId"));
                list = ysjService.findAll("01",dateFrom,dateTo," and pointId = "+pointId,start,limit);
            }else if("jsjRangeData".equals(command)){                
            
                int yearFrom = Integer.parseInt(request.getParameter("yearFrom"));
                int quarterFrom = Integer.parseInt(request.getParameter("quarterFrom"));
                int yearTo = Integer.parseInt(request.getParameter("yearTo"));
                int quarterTo = Integer.parseInt(request.getParameter("quarterTo"));
                
                long pointId = Long.parseLong(request.getParameter("pointId"));
                list = ysjService.findAllJsj("01",yearFrom,quarterFrom,yearTo,quarterTo," and pointId = "+pointId,start,limit,lock);
            }else if("nsjRangeData".equals(command)){                
            
                int yearFrom = Integer.parseInt(request.getParameter("yearFrom"));                
                int yearTo = Integer.parseInt(request.getParameter("yearTo"));               
                
                long pointId = Long.parseLong(request.getParameter("pointId"));
                list = ysjService.findAllNsj("01",yearFrom,yearTo," and pointId = "+pointId,start,limit,lock);
            }else if("extreme".equals(command)){
                String dateFromStr = request.getParameter("dateFrom");
                String dateToStr = request.getParameter("dateTo");
                
                long pointId = Integer.parseInt(request.getParameter("pointId"));
                
                list = ysjService.findAllByExtreme(pointId,dateFromStr,dateToStr,lock);
            }else if("range".equals(command)){
                String dayFromStr = request.getParameter("dayFrom");
                String dayToStr = request.getParameter("dayTo");                                
                long pointId = Integer.parseInt(request.getParameter("pointId"));                
                list = ysjService.findAllRangeByPointId(pointId,dayFromStr,dayToStr);
            }else if("same".equals(command)){
                long pointId = Integer.parseInt(request.getParameter("pointId"));                
                list = ysjService.findAllSameDayByPointId(pointId);
            }else if("jsjSame".equals(command)){
                long pointId = Integer.parseInt(request.getParameter("pointId"));                
                list = ysjService.findAllJsjSameDayByPointId(pointId,lock);
            }else if("generalQuarter".equals(command)){
            
                int year = Integer.parseInt(request.getParameter("yearTo"));
                int quarter = Integer.parseInt(request.getParameter("quarterTo"));
                String condition = TheUtil.getPointSelectCondition(request);
                list = ysjService.findAllJsj(currentOrgCode,year,quarter,year,quarter,condition,start,limit,lock);
            }else if("generalYear".equals(command)){                
                int year = Integer.parseInt(request.getParameter("yearTo"));                
                String condition = TheUtil.getPointSelectCondition(request);
                list = ysjService.findAllNsj(currentOrgCode,year,year,condition,start,limit,lock);
            }else if("ysjOrg".equals(command)){  //单位月数剧
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String year = request.getParameter("yearTo");
                String month=request.getParameter("monthTo");
                String date = year+month;
                list=ysjService.findAllOrgYsjV2(currentOrgCode,date,range,reportStatus,lock);
            }else if("ysjLockOrg".equals(command)){  //单位月数剧 Lock表
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String year = request.getParameter("yearTo");
                String month=request.getParameter("monthTo");
                String date = year+month;
                list=ysjService.findAllOrgYsjLockV2(currentOrgCode,date,range,reportStatus);
            }else if("ysjRealOrg".equals(command)){     //shouy的月合格率走势           
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                list = ysjService.findAllOrgYsjTheYear(currentOrgCode,range,reportStatus);
            }else if("ysjSubOrg".equals(command)){  //下级单位月数剧                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String year = request.getParameter("yearTo");
                String month=request.getParameter("monthTo");
                String date = year+month;
                list=ysjService.findAllSubOrgYsjV2(currentOrgCode,date,range,reportStatus,lock);
            }else if("ysjSubOrgAlarm".equals(command)){  //下级单位低于合格率的月数据                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String year = request.getParameter("yearTo");
                String month=request.getParameter("monthTo");
                String date = year+month;
                list=ysjService.findAllSubOrgYsjAlarmV2(currentOrgCode,date,range,reportStatus,lock);
            }else if("ysjLockSubOrg".equals(command)){  //下级单位月数剧                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String year = request.getParameter("yearTo");
                String month=request.getParameter("monthTo");
                String date = year+month;
                list=ysjService.findAllSubOrgYsjLock(currentOrgCode,date,range,reportStatus);
            }else if("ysjRangeOrg".equals(command)){  //单位月数剧环比            
                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String yearFrom = request.getParameter("yearFrom");
                String monthFrom=request.getParameter("monthFrom");
                String dateFrom = yearFrom+monthFrom;
                
                String yearTo = request.getParameter("yearTo");
                String monthTo=request.getParameter("monthTo");
                String dateTo = yearTo+monthTo;                
                list=ysjService.findAllOrgYsjV2(currentOrgCode,dateFrom,dateTo,range,reportStatus,start,limit,lock);
            }else if("ysjOrgSame".equals(command)){  //单位月数剧同比     
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String yearFrom = request.getParameter("yearFrom");
                
                String yearTo = request.getParameter("yearTo");
                String month=request.getParameter("monthTo");               
                
                list=ysjService.findAllYsjSameV2(currentOrgCode,yearFrom,yearTo,month,range,reportStatus,lock);
            }else if("jsjOrg".equals(command)){  //单位季度汇总数剧
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                        
                String year = request.getParameter("yearTo");
                String quarter=request.getParameter("quarterTo");
                String date = year+quarter;
                list=ysjService.findAllOrgJsjV2(currentOrgCode,date,range,reportStatus,lock);
            }else if("jsjSubOrg".equals(command)){  //下级单位月数剧   
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String year = request.getParameter("yearTo");
                String quarter=request.getParameter("quarterTo");
                String date = year+quarter;
                list=ysjService.findAllSubOrgJsj(currentOrgCode,date,range,reportStatus);
            }else if("jsjRangeOrg".equals(command)){  //单位月数剧环比
            
                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String yearFrom = request.getParameter("yearFrom");
                String quarterFrom=request.getParameter("quarterFrom");
                String dateFrom = yearFrom+quarterFrom;
                
                String yearTo = request.getParameter("yearTo");
                String quarterTo=request.getParameter("quarterTo");
                String dateTo = yearTo+quarterTo;                
                list=ysjService.findAllOrgJsjV2(currentOrgCode,dateFrom,dateTo,range,reportStatus,"",start,limit,lock);
            }else if("jsjOrgSame".equals(command)){  //单位月数剧同比     
                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String yearFrom = request.getParameter("yearFrom");                
                String yearTo = request.getParameter("yearTo");
                String quarter=request.getParameter("quarterTo");               
                
                list=ysjService.findAllJsjSameV2(currentOrgCode,yearFrom,yearTo,quarter,range,reportStatus,lock);
            }else if("nsjOrg".equals(command)){  //单位季度汇总数剧
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                        
                String year = request.getParameter("yearTo");
                
                list=ysjService.findAllOrgNsjV2(currentOrgCode,year,range,reportStatus,lock);
            }else if("nsjSubOrg".equals(command)){  //下级单位年数剧   
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);                
                String year = request.getParameter("yearTo");                
                list=ysjService.findAllSubOrgNsjV2(currentOrgCode,year,range,reportStatus,lock);
            }else if("nsjRangeOrg".equals(command)){  //单位月数剧环比
            
                
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                
                String yearFrom = request.getParameter("yearFrom");
                
                
                String yearTo = request.getParameter("yearTo");
                
                list=ysjService.findAllOrgNsjV2(currentOrgCode,yearFrom,yearTo,range,reportStatus,"",start,limit,lock);
            }else if("ysjReportOrg".equals(command)){  //综合合格率月报表
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String voltageLevelIdParam = request.getParameter("voltageLevelId");
                int voltageLevelId = CommonWebUtil.getIntParameter(voltageLevelIdParam, -1);
                String yearTo = request.getParameter("yearTo");
                String monthTo = request.getParameter("monthTo");
                
                String dateTo = yearTo+monthTo;
                String dateFrom = yearTo + "01";
                //System.out.println("*"+dateTo+"*"+dateFrom);
                //list=ysjService.findAllOrgYsjReportV2(currentOrgCode,dateFrom,dateTo,range,reportStatus,lock);
                list = ysjService.findAllOrgYsjReportV3(currentOrgCode, dateFrom, dateTo, range, reportStatus, voltageLevelId, lock);
            }else if("quarterReportOrg".equals(command)){  //综合合格率月报表
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String yearTo = request.getParameter("yearTo");
                String quarterTo = request.getParameter("quarterTo");
                
                
                String dateTo = yearTo+quarterTo;
                String dateFrom = yearTo + "1";
                //System.out.println("*"+dateTo+"*"+dateFrom);
                list=ysjService.findAllOrgJsjReportV2(currentOrgCode,dateFrom,dateTo,range,reportStatus,lock);
            }else if("nsjReportOrg".equals(command)){  //综合合格率年报表
                String reportStatusParam = request.getParameter("reportStatus");
                int reportStatus = CommonWebUtil.getIntParameter(reportStatusParam, -1);
                
                String rangeParam= request.getParameter("range");
                int range = CommonWebUtil.getIntParameter(rangeParam,-1);
                String yearTo = request.getParameter("yearTo");       
                
                Date now=  new Date();
                String thisMonth = DateUtil.get(now, "MM");
                String thisYear = DateUtil.get(now,"yyyy");
                if(!yearTo.equals(thisYear)){
                    thisMonth = "12";
                }
                String dateTo = yearTo+thisMonth;
                String dateFrom = yearTo + "01";
                //System.out.println("*"+dateTo+"*"+dateFrom);
                list=ysjService.findAllOrgYsjReportV2(currentOrgCode,dateFrom,dateTo,range,reportStatus,lock);
            }else if ("monthException".equals(command)){
                
                String dateFrom = request.getParameter("dateFrom");
                String dateTo = request.getParameter("dateTo");
                String cond  = TheUtil.getPointSelectCondition(request);
                list = ysjService.findAllRateException(currentOrgCode, dateFrom, dateTo, cond, start, limit, lock);
            }else if("yTimeCard".equals(command)){
                
                String yearTo = request.getParameter("yearTo");                
                String cond  = TheUtil.getPointSelectCondition(request);
                list = ysjService.findAllTimeCard(currentOrgCode, yearTo, cond, start, limit, lock);
            }else if("voltageCheck".equals(command)){     //检查电压值是否正常
                String dateFrom = request.getParameter("dateFrom");                
                list = ysjService.findAllVoltageException(currentOrgCode, dateFrom,dateFrom, start, limit, lock);
            }else if("nullCheck".equals(command)){          //检查是否有空值
                String dateFrom = request.getParameter("dateFrom"); 
                list = ysjService.findAllNullException(currentOrgCode, dateFrom, dateFrom, start, limit, lock);
            }else if("runtimeCheck".equals(command)){      //检查是否运行时过低
                int valve = CommonWebUtil.getIntParameter(request.getParameter("runtime"), 1000);
                String dateFrom = request.getParameter("dateFrom");
                list  = ysjService.findAllLowRunimeException(currentOrgCode, dateFrom, dateFrom,valve, start, limit, lock);
            }else if("dateCheck".equals(command)){ //检查最大值时，最小值时日期是否合理
                String dateFrom = request.getParameter("dateFrom");
                int startDay=20; //统计起始日
                list  = ysjService.findAllDateException(currentOrgCode, dateFrom, dateFrom,startDay, start, limit, lock);
            }else if("timeCheck".equals(command)){ //运行时检查，合格时，超上时，超下时
                String dateFrom = request.getParameter("dateFrom");
                int startDay=20; //统计起始日
                list  = ysjService.findAllTimeException(currentOrgCode, dateFrom, dateFrom, start, limit, lock);
            }
            
            String jsonText;
            jsonText = JsonUtil.JavaMapListToGridFormat("data", list, ysjService.getSize(),otherProperties);
            out.printf(jsonText);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
